import { reqCategory1, reqCategory2, reqCategory3 } from '@/api/Category'
import type { IResCategory } from '@/types/category'
import { defineStore } from 'pinia'

interface ICategoryState {
  category1Id: string | number
  category2Id: string | number
  category3Id: string | number
  category1List: IResCategory[]
  category2List: IResCategory[]
  category3List: IResCategory[]
}

// 三级分类的 Store
export const useCategoryStore = defineStore('category', {
  state: (): ICategoryState => {
    return {
      category1Id: '', // 一级分类 ID
      category2Id: '', // 二级分类 ID
      category3Id: '', // 三级分类 ID
      category1List: [], // 一级分类 列表数据
      category2List: [], // 二级分类 列表数据
      category3List: [] // 三级分类  列表数据
    }
  },

  actions: {
    // 获取一级分类数据
    async getCategory1List() {
      // 调用接口 API
      const res = await reqCategory1()

      // 获取数据成功后进行赋值
      if (res.code === 200) {
        this.category1List = res.data
      }
    },
    // 获取二级分类数据
    async getCategor2List(id: number) {
      const res = await reqCategory2(id)
      if (res.code === 200) {
        this.category2List = res.data
      }
    },
    // 获取三级分类数据
    async getCategor3List(id: number) {
      const res = await reqCategory3(id)

      if (res.code === 200) {
        this.category3List = res.data
      }
    }
  }
})
